package com.mf.all.controller.customer;


import cn.dev33.satoken.stp.StpUtil;
import com.mf.all.constant.ConstantBasic;
import com.mf.all.emuns.enumsPublic.EnumPublicEnableOrDisable;
import com.mf.all.entity.req.publicReq.updateStatus.ReqPublicIntIdUpdateStatus;
import com.mf.all.groups.GroupsAddInterface;
import com.mf.all.groups.GroupsUpdateInterface;
import com.mf.all.result.Result;
import com.mf.all.utils.UtilController;
import com.mf.all.utils.UtilsEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.all.entity.dto.DtoCustomer;
import com.mf.all.entity.req.customer.ReqCustomer;
import com.mf.all.service.ServiceCustomer;
import com.mf.all.entity.req.search.customer.SearchReqCustomer;

import java.util.List;

/**
 * 客户
 *
 * @date 2025-04-02 22:36:47
 */
@RestController
@RequestMapping("customer")
@Tag(name = "客户")
@Slf4j
public class ControllerCustomer {
    @Resource
    private ServiceCustomer servicecustomer;

    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    @SaCheckPermission("customer:search")
    public Result<Page<DtoCustomer>> findPage(@RequestBody SearchReqCustomer searchReqCustomer) {
        return Result.success(servicecustomer.findPageByParams(searchReqCustomer));
    }

    @Operation(summary = "列表查询")
    @PostMapping("/list")
    @SaCheckPermission("customer:search")
    public Result<List<DtoCustomer>> list(@RequestBody SearchReqCustomer searchReqCustomer) {
        return Result.success(servicecustomer.findListByParams(searchReqCustomer));
    }

    @Operation(summary = "保存")
    @PostMapping("/save")
    @SaCheckPermission("customer:save")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqCustomer reqCustomer) {
        servicecustomer.save(UtilController.setBo(reqCustomer, DtoCustomer.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @SaCheckPermission("customer:update")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqCustomer reqCustomer) {
        // 不可修改客户全称
        if (!StpUtil.hasRole(ConstantBasic.ADMIN_NAME)) {
            reqCustomer.setCName(null);
        }
        servicecustomer.updateById(UtilController.setBo(reqCustomer, DtoCustomer.class));
        return Result.success();
    }

    @Operation(summary = "客户状态修改")
    @PostMapping("/updateStatus")
    @SaCheckPermission("customer:status")
    public Result<Void> delete(@Validated @RequestBody ReqPublicIntIdUpdateStatus updateStatus) {
        UtilsEnum.existsInEnum(EnumPublicEnableOrDisable.class, updateStatus.getStatus());
        servicecustomer.updateById(UtilController.setBo(updateStatus, DtoCustomer.class));
        return Result.success();
    }
}
